<?php

declare(strict_types=1);
/**
 * Author: Zane
 * Email: 873934580@qq.com
 * Date: 2022/6/6.
 */

namespace Zane\View\Compiler;

const TAG_NO_STATE = 0;
const TAG_STATE = 1;

trait ParseTrait
{
    private $start;
    /**
     * 当前参数类型.
     *
     * @var int
     *          类型说明：
     *          0：字符串
     *          1：数组
     *          2：变量
     */
    private $currentParamType = 0;
    /**
     * @var string 当前找到的标志
     */
    private $tagChar;

    /**
     * @var int 当前循环状态
     *          状态说明：
     *          0    未找到标志
     *          1    已找到开始标志 ，等待结束标志
     */
    private $currentState = TAG_NO_STATE;

    private $temp = '';

    /**
     * 参数解析.
     */
    protected function parse(): void
    {
        $len = mb_strlen($this->expression, $this->encoding);

        $tag = '';
        $result = [];

        for ($i = 0; $i < $len; ++$i) {
            $char = mb_substr($this->expression, $i, 1, $this->encoding);
            // 当字符为空，并且没有找到标志位时，则认为字符无效
            if (blank($char) && TAG_NO_STATE === $this->currentState) {
                continue;
            }
            // 当没有找到标志位置时，找到起始符号
            if ($this->startTag($char) && TAG_NO_STATE === $this->currentState) {
                continue;
            }
            if ($this->endTag($char)) {
                $result[$tag] = $this->temp;
                $this->temp = '';

                continue;
            }

            if ($this->symbol($char) && TAG_NO_STATE === $this->currentState) {
                $tag = $this->temp;
                $result[$tag] = '';
                $this->temp = '';
                if (blank($this->tagChar)) {
                    $this->tagChar = ',';
                }

                continue;
            }
            // dump('赋值前'.$this->temp.'==='.$char);
            if ('$' === $char && $this->temp === $char) {
                continue;
            }
            $this->temp .= $char;
            // dump('赋值后'.$this->temp.'==='.$char);
        }

        if ('' !== $tag && '' !== $this->temp) {
            $result[$tag] = $this->temp;
            $this->temp = '';
        }

        $this->result = $result;
    }

    private function symbol($char): bool
    {
        if ('=' === $char) {
            return true;
        }

        return false;
    }

    /**
     * 开始位标签.
     * 3种情况
     * 1、字符串
     * 2、数组
     * 3、变量.
     *
     * @param $char
     *
     * @return bool
     */
    private function startTag($char): bool
    {
        if (',' === $char) {
            return true;
        }
        if ('"' === $char || "'" === $char) {
            $this->currentState = TAG_STATE;
            $this->tagChar = $char;
            $this->currentParamType = 0;

            return true;
        }

        // 处理数组类型
        if ('[' === $char) {
            $this->currentState = TAG_STATE;
            $this->temp = '[';
            $this->tagChar = ']';
            $this->currentParamType = 1;

            return true;
        }

        // 处理变量类型
        if ('$' === $char) {
            $this->currentState = TAG_STATE;
            $this->temp = $char;
            $this->currentParamType = 2;

            return true;
        }

        return false;
    }

    /**
     * 结束标签.
     *
     * @param $char
     *
     * @return bool
     */
    private function endTag($char): bool
    {
        if ($this->tagChar === $char && 1 === $this->currentState) {
            $this->tagChar = '';
            $this->currentState = 0;
            if (']' === $char) {
                $this->temp .= ']';
            }

            return true;
        }

        return false;
    }
}
